package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

import static com.sky.constant.RedisConstant.DISH_KEY;

/**
 * 菜品相关接口
 */
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;
    private static final String CLEAN_PATTERN_KEY=DISH_KEY+"*";


    /**
     * 新增菜品和口味
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品和口味")
    @PostMapping
    public Result saveWithFlavor(@RequestBody DishDTO dishDTO){
        log.info("新增菜品和口味：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理指定套餐缓存数据
        String key=DISH_KEY+dishDTO.getCategoryId();
        cleanDishCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    /*
    * 为什么不加@RequestParam进行查询参数的接收？
    * 答：springboot自动借助`@RequestParam`注解进行字段匹配
     * */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult=dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    /*
    * RequestParam注解的作用，解析1,2,3数组并放入List集合中
    * */
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}",ids);
        dishService.deleteBatch(ids);

        //清理所有菜品缓存
        cleanDishCache(CLEAN_PATTERN_KEY);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    /*
    * @PathVariable注解的作用，获取?id=xx并赋值到id中
    * 注意：拼接的id必须和参数名一致
    * */
    public Result getById(@PathVariable Long id){
        log.info("菜品id为：{}",id);
        DishVO dishVO=dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 保存修改的菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改的菜品：{}",dishDTO);
        dishService.update(dishDTO);

        //清理所有菜品缓存
        cleanDishCache(CLEAN_PATTERN_KEY);
        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品待修改的状态为：{}",status);
        dishService.startOrStop(status,id);

        //清理所有菜品缓存
        cleanDishCache(CLEAN_PATTERN_KEY);
        return Result.success();
    }

    /**
     * 清理菜品缓存
     */
    private void cleanDishCache(String pattern){
        //将所有的菜品缓存数据清理掉，所有以dish_开头的key
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping ("/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId){
        log.info("分类id:{}",categoryId);
        List<Dish> list= dishService.getByCategoryId(categoryId);
        return Result.success(list);
    }
}
